<?php
/**
 * 
 * @author yuxun
 *
 */

class ModOssActivity extends Model {

    protected $table = 'activity';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }

    public function addmuch($params){
    	$result = $this->getDbConnection()->multiInsert($params, $this->table, FALSE);
    	return $result;
    }
    
    public function select($params){
    	$table = $this->table;
    	$where  = ' WHERE game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    
        if(!empty($params['activity_id'])){
    		$where .= " AND ac.activity_id like :activity_id ";
    		$args['activity_id'] = '%'.$params['activity_id'].'%';
    	} 

    	if(!empty($params['activity_name'])){
    		$where .= " AND ac.activity_name like :activity_name ";
    		$args['activity_name'] = '%'.$params['activity_name'].'%';
    	}
    	
    	if(!empty($params['classify'])){
    		$where .= " AND ac.classify like :classify ";
    		$args['classify'] = $params['classify'];
    	}

    	$fields = "a.id,a.category_id,a.postpone,a.start_time,a.create_user,a.create_time,a.is_open,a.synchro_time,a.status,ac.activity_id,ac.activity_name,ac.classify,ac.duration";
    	$sql = "SELECT {$fields} FROM {$table} as a inner join activity_category as ac on ac.id = a.category_id {$where}";
    	$data = $this->getDbConnection()->query($sql, $args);    	
    	return $data;
    }
    
    public function page($params){
    	$table = $this->table;
    	$where  = ' WHERE a.game=:game ';
    	$args   = array(
    			':game'     => $params['g']
    	);
    	
        if(!empty($params['activity_id'])){
    		$where .= " AND ac.activity_id like :activity_id ";
    		$args['activity_id'] = '%'.$params['activity_id'].'%';
    	} 

    	if(!empty($params['activity_name'])){
    		$where .= " AND ac.activity_name like :activity_name ";
    		$args['activity_name'] = '%'.$params['activity_name'].'%';
    	}
    	
    	if(!empty($params['classify'])){
    		$where .= " AND ac.classify like :classify ";
    		$args['classify'] = $params['classify'];
    	}
    	
    	$c_sql = "select count(1) as cnt from {$table} as a inner join activity_category as ac on ac.id = a.category_id  {$where}";
    	
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;

    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "a.id,a.category_id,a.postpone,a.start_time,a.create_user,a.create_time,a.is_open,a.synchro_time,a.status,ac.activity_id,ac.activity_name,ac.classify,ac.duration";
    	$sql = "SELECT {$fields} FROM {$table} as a inner join activity_category as ac on ac.id = a.category_id {$where} {$order} {$limit}";   	
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	$order[0] = 'a.'.$order[0];
    	return " ORDER BY {$order[0]} {$sort} ";
    }
    
    
    public function getInfo($id){
    	$table = $this->table;
        if(!empty($id)){
    		$where .= " where a.id = :id ";
    		$args['id'] = $id;
    	} 
    	$fields = "a.id,a.category_id,a.postpone,a.start_time,a.create_user,a.create_time,a.is_open,a.synchro_time,a.status,ac.activity_id,ac.activity_name,ac.classify,ac.duration";
    	$sql = "SELECT {$fields} FROM {$table} as a inner join activity_category as ac on ac.id = a.category_id {$where}";
    	$data = $this->getDbConnection()->query($sql, $args);
    	return $data;
    }
    
    public function getActivityForIds($ids){
    	$table = $this->table;
    	$fields = "a.id,a.category_id,a.postpone,a.start_time,a.create_user,a.create_time,a.is_open,a.synchro_time,a.status,ac.activity_id,ac.activity_name,ac.classify,ac.duration";
    	$sql = "SELECT {$fields} FROM {$table} as a inner join activity_category as ac on ac.id = a.category_id where a.id in ({$ids}) ";
    	$data = $this->getDbConnection()->query($sql, $args);
    	return $data;
    }
    
    public function repeat($id,$field,$val,$g){
    	$sql = "SELECT count(1) as cnt FROM {$this->table} where id != {$id} and {$field} = '{$val}' and game = '{$g}' and status in(1,2)";
    	//echo $sql;exit;
    	$c_result = $this->getDbConnection()->getAll($sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;
    	return $count;
    }
    
    
    
} 